<template>
  <div>
    <div class="head">
      <div class="input">
        <span>文章标题：</span>
        <el-input v-model="queryData.title" placeholder="请输入" />
      </div>
      <div class="input">
        <span>文章分类：</span>
        <el-select v-model="queryData.type_id">
          <el-option :label="item.type_name" :value="item.id" v-for="(item, index) in type_list" :key="item.id"></el-option>
        </el-select>
      </div>
      <div class="input">
        <span>查看权限：</span>
        <el-select v-model="queryData.from_type">
          <el-option label="所有人" :value="0">所有人</el-option>
          <el-option label="企业" :value="1">企业</el-option>
          <el-option label="员工" :value="2">员工</el-option>
        </el-select>
      </div>
      <div class="input">
        <span>是否置顶：</span>
        <el-select v-model="queryData.top_status">
          <el-option label="置顶" :value="1">置顶</el-option>
          <el-option label="不置顶" :value="2">不置顶</el-option>
        </el-select>
      </div>
      <el-button type="primary" class="btn" @click="search">查询</el-button>
      <el-button type="primary" class="btn" @click="reset">重置</el-button>
      <el-button type="primary" class="btn" @click="add">添加</el-button>
    </div>
    <div class="box">
      <el-table
        :data="tableData"
        border
        v-loading="isTableLoading"
        empty-text="暂无数据"
        no-filtered-userFrom-text="暂无筛选结果"
        style="width: 100%"
      >
        <el-table-column fixed type="index" align="center" label="序号" width="50" />
        <el-table-column fixed prop="title" align="center" label="标题" min-width="120" />
        <el-table-column fixed prop="type.type_name" align="center" label="分类" min-width="120" />
        <el-table-column fixed prop="from_type" align="center" label="查看权限" min-width="100" >
          <template slot-scope="scope">
            <p v-if="scope.row.from_type==0">所有人</p>
            <p v-else-if="scope.row.from_type==1">企业</p>
            <p v-else-if="scope.row.from_type==2">员工</p>
          </template>
        </el-table-column>
        <el-table-column fixed prop="top_status" align="center" label="是否置顶" min-width="80" >
          <template slot-scope="scope">
            <p v-if="scope.row.top_status==1">置顶</p>
            <p v-else-if="scope.row.top_status==2">不置顶</p>
          </template>
        </el-table-column>
        <el-table-column fixed prop="create_time" align="center" label="添加时间" min-width="120" />
        <!--<el-table-column fixed prop="see_total" align="center" label="浏览数" min-width="120" />-->
        <el-table-column fixed="right" align="center" prop="action" label="操作" width="120">
          <template #default="scope">
            <div class="action">
              <span @click="edit(scope)">编辑</span>
              <span @click="del(scope)">删除</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="acea-row row-right page">
      <el-pagination
        style="margin-top: 5px"
        @size-change="search"
        @current-change="search"
        :current-page.sync="queryData.page"
        :page-size.sync="queryData.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      </div>
    </div>

    <Edit ref="edit" />

    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>确定删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="delOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/libs/request'
import Edit from './edit.vue'

export default {
  name: 'articleList',
  components: {
    Edit
  },
  data() {
    return {
      queryData: {
        title: '',
        type_id: '',
        from_type: '',
        top_status: '',
        page: 1,
        size: 10,
      },
      type_list: [],
      total: 0,
      isTableLoading: false,
      tableData: [],
      dialogVisible: false,
    };
  },
  computed: {},
  created() {},
  mounted() {
    this.getList();
    this.getInfo();
  },
  methods: {
    getInfo() {
      request({
        url: '/article/index/info',
        method:'post',
      }).then(res => {
        this.type_list = res.data.cates
      })
    },
    search(d) {
      if (typeof d == 'number') {
        this.queryData.page = d;
      } else {
        this.queryData.page = 1;
      }
      this.getList();
    },
    reset() {
      this.queryData.title = '';
      this.queryData.page = 1;
      this.queryData.type_id = '';
      this.queryData.from_type = '';
      this.queryData.top_status = '';
      this.search();
    },
    edit(item) {
      this.$refs.edit.showModal(item.row.id)
    },
    del(item) {
      this.item = item;
      this.$confirm('确定要删除文章吗？', '提醒', {
        type: 'warning',
      }).then(res => {
        this.delOk()
      }).catch(err => {
      })
    },
    delOk() {
      request({
        url: 'article/index/delete?id=' + this.item.row.id,
        method: 'post',
      }).then(res => {
        console.log(res);
        this.$message.success('删除成功')
        this.$delete(this.tableData, this.item.$index);
        this.dialogVisible = false;
      }).catch(err => {
        this.$message.error(err.msg)
        this.dialogVisible = false;
      })
    },
    add() {
      this.$refs.edit.showModal()
    },
    getList() {
      this.isTableLoading = true;
      this.tableData = [];
      this.total = 0;
      request({
        url: 'article/index/list',
        data: this.queryData,
        method: 'post',
      }).then(res => {
        this.tableData = res.data.list
        this.total = res.data.total
      })
      this.isTableLoading = false;
    },
  },
};
</script>

<style scoped>
.head {
  width: 100%;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  background: #ffffff;
  border-radius: 5px;
  padding: 20px;
  padding-bottom: 0px;
}

.input {
  display: flex;
  align-items: center;
  margin-right: 15px;
  margin-bottom: 20px;
}

.input > span {
  font-size: 12px;
  flex-shrink: 0;
}

.btn {
  margin-bottom: 20px;
}

.box {
  width: 100%;
  background: #ffffff;
  border-radius: 5px;
  padding: 20px;
  margin-top: 20px;
}

.action {
  display: flex;
  align-items: center;
  justify-content: center;
}

.action span {
  color: #0256ff;
  font-size: 12px;
  cursor: pointer;
  margin: 0 8px;
}
</style>
